package emailserving.config;


import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.EbeanServerFactory;
import com.avaje.ebean.config.PersistBatch;
import com.avaje.ebean.config.ServerConfig;
import emailserving.component.EmailSender;
import emailserving.component.EmailSenderImpl;
import org.apache.commons.dbcp2.BasicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.task.TaskExecutor;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Configuration()
@Import(
        {WebMvcConfig.class, AppMetadataConfig.class}
)
public class MainConfig {
    private static final Logger LOG = LoggerFactory.getLogger(MainConfig.class);

    @Autowired
    AppMetadataConfig appMetadataConfig;

    @Bean
    public EmailSender emailSender() {
        return new EmailSenderImpl(
                appMetadataConfig.getEmailServerHost(),
                appMetadataConfig.getEmailServerPort(),
                appMetadataConfig.getEmailServerUserName(),
                appMetadataConfig.getEmailServerPassword());
    }

    @Bean
    public EbeanServer ebeanServer() {
        ServerConfig config = new ServerConfig();
        config.setName("db");
        config.setCurrentUserProvider(() -> "mysql");
        config.setDatabasePlatformName(appMetadataConfig.getDatabasePlatformName());
        config.setDataSource(dataSource());
        config.setDdlGenerate(appMetadataConfig.getDB_DDL_generate());
        config.setDdlRun(appMetadataConfig.getDB_DDL_run());
        config.setPersistBatching(appMetadataConfig.getDB_persist_batching());
        config.setDefaultServer(true);
        config.setRegister(true);
        return EbeanServerFactory.create(config);
    }

    @Bean
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(appMetadataConfig.getJdbcDriverClassName());
        dataSource.setUrl(appMetadataConfig.getJdbcURL());
        dataSource.setUsername(appMetadataConfig.getJdbcUsername());
        dataSource.setPassword(appMetadataConfig.getJdbcPassword());
        dataSource.setInitialSize(appMetadataConfig.getJdbcConnectionPoolInitialSize());
        dataSource.setMaxTotal(appMetadataConfig.getJdbcConnectionPoolMaxTotalSize());
        return dataSource;
    }

    @Bean
    public ExecutorService executorService(){
        return Executors.newFixedThreadPool(30);
    }

    @PostConstruct
    public void initApp() {
        LOG.debug("Looking for Spring profiles...");

        /**
         * enhance ebean model
         */
//        if (!AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent",
//                "debug=1;packages=net.oschina.ml.domain.**")) {
//            LOG.info("avaje-ebeanorm-agent not found in classpath - not dynamically loaded");
//        }
    }

}
